import { cloneDeep, lodashMap as map, template as lodashTemplate } from "lodash-es";
import { failres, okres } from "../../config/resUtil.js";

import { Controller, App, Post, Body } from "@midwayjs/core";
import { Application } from "../../interface.js";

// 一些默认的公共的操作
export class tb_taskController {
  @App("koa")
  app: Application;


  async querybuildcomon(query, body, ispage = true) {

    if (body?.["searchkey"]) {
      query.where(function () {
        this.whereLike("taskname", `%${body?.["searchkey"]}%`)
        .orWhereLike("taskcontent", `%${body?.["searchkey"]}%`);
      });
    }


    if (body?.["id"]) {
      query.andWhere("id", body?.["id"]);
    }

    if (body?.["parent_id"]) {
      query.andWhere("parent_id", body?.["parent_id"]);
    }


    if (body?.["taskname"]) {
      query.andWhere("taskname", body?.["taskname"]);
    }

    if (body?.["taskcontent"]) {
      query.andWhere("taskcontent", body?.["taskcontent"]);
    }

    if (body?.["real_starttime"]) {
      query.andWhere("real_starttime", body?.["real_starttime"]);
    }

    if (body?.["plan_endtime"]) {
      query.andWhere("plan_endtime", body?.["plan_endtime"]);
    }

    if (body?.["plan_work_time"]) {
      query.andWhere("plan_work_time", body?.["plan_work_time"]);
    }


    if (body?.["plan_endtime_start"]) {
      query.andWhere("plan_endtime", ">=", body?.["plan_endtime_start"]);
    }

    if (body?.["plan_endtime_end"]) {
      query.andWhere("plan_endtime", "<=", body?.["plan_endtime_end"]);
    }


    if (body?.["state"]) {
      query.andWhere("state", "=", body?.["state"]);
    }


    if (ispage) {
      if (body?.pageSize) {
        query.limit(body?.pageSize);
      }


      var pageNum = body?.pageNum || 1;
      if (pageNum) {
        query.offset((pageNum - 1) * body?.pageSize);
      }
    }


  }

  getDb() {
    return this.app["sqiltedb"];
  }


  async getlist(body) {
    console.log("开始 列表 ");

    var query = this.getDb().select().from("tb_task");
    var query1 = this.getDb().select().from("tb_task");

    //获取默认字段
    this.querybuildcomon(query, body);
    query.orderBy([{ column: "order_num", order: "asc" }]);
    this.querybuildcomon(query1, body, false);
    var totalresults = await query1.count("id");
    var total = totalresults?.[0]?.["count(`id`)"];
    console.log("total :", total);


    return okres(await query, { total });
  }

  async getbyitem(body) {
    console.log("开始 获取单个 id:", body?.id);
    var query = this.getDb().select("*").from("tb_task");
    console.log("body :", body);


    if (!body?.id) {
      return okres(null);
    }

    if ((!body) || !Object.keys(body).length) {
      return okres(null);
    }

    if (body?.id) {
      query.andWhere("id", "=", body?.id);
    } else {

    }


    console.log("query :", query);

    var lists = await query;
    var one = lists?.[0];
    console.log("开始sql  :", one);

    //获取明细数据
    if (one?.id) {
      var detailsList_rs = await this.getlist({ parent_id: one?.id });
      one.detailsList = detailsList_rs?.data || [];
    }


    return okres(one);
  }

  async 删除(body) {
    if (!body.id) {
      return failres("没有id");
    }
    var delresult = await this.getDb()("tb_task").where("id", body.id).del();
    return okres(delresult);
  }

  async addorupdate(body) {
    console.log("开始 新增or修改 ");
    var rsiltonme = await this.getbyitem(body);
    var one = rsiltonme && rsiltonme.data;
    console.log("one :", one);

    var result = undefined;
    if (one?.id) {
      var __params = cloneDeep(body);
      var detailsList = __params?.detailsList || [];
      delete __params.id;
      delete __params.detailsList;

      var resultid = one?.id;
      result = await this.getDb()("tb_task").where("id", "=", resultid).update(__params);

      //for 循环明细
      for (let i = 0; i < detailsList.length; i++) {
        detailsList[i].parent_id = resultid;
        await this.addorupdate(detailsList[i]);
      }
    } else {
      // 新增
      var __params = cloneDeep(body);
      var detailsList = __params?.detailsList || [];
      delete __params.id;
      delete __params.detailsList;
      resultid = (await this.getDb()("tb_task").insert(__params))?.[0];

      //for 循环明细
      for (let i = 0; i < detailsList.length; i++) {
        detailsList[i].parent_id = resultid;
        await this.addorupdate(detailsList[i]);
      }

    }
    return okres({ id: resultid });
  }

}
